//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.lyyzoo.util;

import com.fasterxml.jackson.databind.util.ClassUtil;
import com.lyyzoo.log.Logs;
import java.io.File;
import java.io.FileFilter;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public abstract class Classes {
    public Classes() {
    }

    public static ClassLoader getClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }

    public static InputStream getResourceAsStream(String resource) {
        return getClassLoader().getResourceAsStream(resource);
    }

    public static InputStream getResourceAsStream(String resource, Class<?> clazz) {
        InputStream is = clazz.getResourceAsStream(resource);
        if (is == null) {
            is = getResourceAsStream(resource);
        }

        return is;
    }

    public static URL getURL(String resource) {
        return getClassLoader().getResource(resource);
    }

    public static URL getURL(String resource, Class<?> clazz) {
        URL url = clazz.getResource(resource);
        if (url == null) {
            url = getURL(resource);
        }

        return url;
    }

    public static String getClassPath() {
        String classpath = "";
        URL resource = getURL("");
        if (resource != null) {
            classpath = resource.getPath();
        }

        return classpath;
    }

    public static String getClassRelativePath(Class<?> clazz) {
        return clazz.getPackage().getName().replace(".", "/");
    }

    public static String getClassPath(Class<?> clazz) {
        return getClassPath() + getClassRelativePath(clazz);
    }

    public static Class<?> loadClass(String className, boolean isInitialized) {
        try {
            Class<?> cls = Class.forName(className, isInitialized, getClassLoader());
            return cls;
        } catch (ClassNotFoundException var4) {
            Logs.error(Classes.class, "加载类出错！", var4);
            throw new RuntimeException(var4);
        }
    }

    public static List<Class<?>> getClassList(String packageName, boolean isRecursive) {
        ArrayList classList = new ArrayList();

        try {
            Enumeration urls = getClassLoader().getResources(packageName.replace(".", "/"));

            while(true) {
                label49:
                while(true) {
                    URL url;
                    do {
                        if (!urls.hasMoreElements()) {
                            return classList;
                        }

                        url = (URL)urls.nextElement();
                    } while(url == null);

                    String protocol = url.getProtocol();
                    if (protocol.equals("file")) {
                        String packagePath = url.getPath();
                        addClass(classList, packagePath, packageName, isRecursive);
                    } else if (protocol.equals("jar")) {
                        JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
                        JarFile jarFile = jarURLConnection.getJarFile();
                        Enumeration jarEntries = jarFile.entries();

                        while(true) {
                            String className;
                            do {
                                String jarEntryName;
                                do {
                                    if (!jarEntries.hasMoreElements()) {
                                        continue label49;
                                    }

                                    JarEntry jarEntry = (JarEntry)jarEntries.nextElement();
                                    jarEntryName = jarEntry.getName();
                                } while(!jarEntryName.endsWith(".class"));

                                className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");
                            } while(!isRecursive && !className.substring(0, className.lastIndexOf(".")).equals(packageName));

                            classList.add(loadClass(className, false));
                        }
                    }
                }
            }
        } catch (Exception var12) {
            Logs.error(Classes.class, "获取类出错！", var12);
            throw new RuntimeException(var12);
        }
    }

    public static List<Class<?>> getClassListByAnnotation(String packageName, Class<? extends Annotation> annotationClass) {
        ArrayList classList = new ArrayList();

        try {
            Enumeration urls = getClassLoader().getResources(packageName.replace(".", "/"));

            while(true) {
                while(true) {
                    URL url;
                    do {
                        if (!urls.hasMoreElements()) {
                            return classList;
                        }

                        url = (URL)urls.nextElement();
                    } while(url == null);

                    String protocol = url.getProtocol();
                    if (protocol.equals("file")) {
                        String packagePath = url.getPath();
                        addClassByAnnotation(classList, packagePath, packageName, annotationClass);
                    } else if (protocol.equals("jar")) {
                        JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
                        JarFile jarFile = jarURLConnection.getJarFile();
                        Enumeration jarEntries = jarFile.entries();

                        while(jarEntries.hasMoreElements()) {
                            JarEntry jarEntry = (JarEntry)jarEntries.nextElement();
                            String jarEntryName = jarEntry.getName();
                            if (jarEntryName.endsWith(".class")) {
                                String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");
                                Class<?> cls = loadClass(className, false);
                                if (cls.isAnnotationPresent(annotationClass)) {
                                    classList.add(cls);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception var13) {
            Logs.error(Classes.class, "获取类出错！", var13);
            throw new RuntimeException(var13);
        }
    }

    public static List<Class<?>> getClassListBySuper(String packageName, Class<?> superClass) {
        ArrayList classList = new ArrayList();

        try {
            Enumeration urls = getClassLoader().getResources(packageName.replace(".", "/"));

            while(true) {
                while(true) {
                    URL url;
                    do {
                        if (!urls.hasMoreElements()) {
                            return classList;
                        }

                        url = (URL)urls.nextElement();
                    } while(url == null);

                    String protocol = url.getProtocol();
                    if (protocol.equals("file")) {
                        String packagePath = url.getPath();
                        addClassBySuper(classList, packagePath, packageName, superClass);
                    } else if (protocol.equals("jar")) {
                        JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
                        JarFile jarFile = jarURLConnection.getJarFile();
                        Enumeration jarEntries = jarFile.entries();

                        while(jarEntries.hasMoreElements()) {
                            JarEntry jarEntry = (JarEntry)jarEntries.nextElement();
                            String jarEntryName = jarEntry.getName();
                            if (jarEntryName.endsWith(".class")) {
                                String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");
                                Class<?> cls = loadClass(className, false);
                                if (superClass.isAssignableFrom(cls) && !superClass.equals(cls)) {
                                    classList.add(cls);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception var13) {
            Logs.error(Classes.class, "获取类出错！", var13);
            throw new RuntimeException(var13);
        }
    }

    private static void addClass(List<Class<?>> classList, String packagePath, String packageName, boolean isRecursive) {
        try {
            File[] files = getClassFiles(packagePath);
            if (files != null) {
                File[] var5 = files;
                int var6 = files.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    File file = var5[var7];
                    String fileName = file.getName();
                    String subPackagePath;
                    if (file.isFile()) {
                        subPackagePath = getClassName(packageName, fileName);
                        classList.add(loadClass(subPackagePath, false));
                    } else if (isRecursive) {
                        subPackagePath = getSubPackagePath(packagePath, fileName);
                        String subPackageName = getSubPackageName(packageName, fileName);
                        addClass(classList, subPackagePath, subPackageName, true);
                    }
                }
            }

        } catch (Exception var12) {
            Logs.error(Classes.class, "添加类出错！", var12);
            throw new RuntimeException(var12);
        }
    }

    private static File[] getClassFiles(String packagePath) {
        return (new File(packagePath)).listFiles(new FileFilter() {
            public boolean accept(File file) {
                return file.isFile() && file.getName().endsWith(".class") || file.isDirectory();
            }
        });
    }

    private static String getClassName(String packageName, String fileName) {
        String className = fileName.substring(0, fileName.lastIndexOf("."));
        if (Strings.isNotEmpty(packageName)) {
            className = packageName + "." + className;
        }

        return className;
    }

    private static String getSubPackagePath(String packagePath, String filePath) {
        String subPackagePath = filePath;
        if (Strings.isNotEmpty(packagePath)) {
            subPackagePath = packagePath + "/" + filePath;
        }

        return subPackagePath;
    }

    private static String getSubPackageName(String packageName, String filePath) {
        String subPackageName = filePath;
        if (Strings.isNotEmpty(packageName)) {
            subPackageName = packageName + "." + filePath;
        }

        return subPackageName;
    }

    private static void addClassByAnnotation(List<Class<?>> classList, String packagePath, String packageName, Class<? extends Annotation> annotationClass) {
        try {
            File[] files = getClassFiles(packagePath);
            if (files != null) {
                File[] var5 = files;
                int var6 = files.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    File file = var5[var7];
                    String fileName = file.getName();
                    String className;
                    if (file.isFile()) {
                        className = getClassName(packageName, fileName);
                        Class<?> cls = loadClass(className, false);
                        if (cls.isAnnotationPresent(annotationClass)) {
                            classList.add(cls);
                        }
                    } else {
                        className = getSubPackagePath(packagePath, fileName);
                        String subPackageName = getSubPackageName(packageName, fileName);
                        addClassByAnnotation(classList, className, subPackageName, annotationClass);
                    }
                }
            }

        } catch (Exception var12) {
            Logs.error(Classes.class, "添加类出错！", var12);
            throw new RuntimeException(var12);
        }
    }

    private static void addClassBySuper(List<Class<?>> classList, String packagePath, String packageName, Class<?> superClass) {
        try {
            File[] files = getClassFiles(packagePath);
            if (files != null) {
                File[] var5 = files;
                int var6 = files.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    File file = var5[var7];
                    String fileName = file.getName();
                    String className;
                    if (file.isFile()) {
                        className = getClassName(packageName, fileName);
                        Class<?> cls = loadClass(className, false);
                        if (superClass.isAssignableFrom(cls) && !superClass.equals(cls)) {
                            classList.add(cls);
                        }
                    } else {
                        className = getSubPackagePath(packagePath, fileName);
                        String subPackageName = getSubPackageName(packageName, fileName);
                        addClassBySuper(classList, className, subPackageName, superClass);
                    }
                }
            }

        } catch (Exception var12) {
            Logs.error(Classes.class, "添加类出错！", var12);
            throw new RuntimeException(var12);
        }
    }

    public static boolean isClassExist(String className) {
        try {
            ClassLoader.getSystemClassLoader().loadClass(className);
            return true;
        } catch (ClassNotFoundException var2) {
            return false;
        }
    }

    public static boolean isResourceExist(String fullName) {
        return ClassUtil.class.getResource(fullName) != null;
    }
}
